<template>
	<view class=" ">
		<u-navbar :title="$params.show?'优惠券':'领券中心'" titleBold :background="{background:'#0000'}">
			<view class="pl20 pr20 flex row-between w-max pr z-index2">
				<view class=""></view>
				<view class="" v-if="$params.show==1" @click="$jump('/pages/my/coupon/coupon',{show:0})">领券中心</view>
				<view class="" v-if="$params.show==0" @click="$jump('receiveCode')">指令领券</view>
			</view>
		</u-navbar>
		<u-tabs :list="tabs" :is-scroll="false" :current="current" @change="e=>current=e" :active-color="$zs"
			inactive-color="#666" v-if="$params.show==1"></u-tabs>
		<u-empty mode="coupon" v-if="!list.length" marginTop="50"></u-empty>
		<view class="p20">
			<view class="mb20" v-for="(item,index) in list" :key="index">
				<coupon :data="item" @success="getList" :receive="$params.show==0" />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 0,
				show: 0,
				list: ''
			};
		},
		computed: {
			tabs() {
				return [{
					name: `未使用(${this.$user.coupon_num||0})`,
					status: 0,
				}, {
					name: `已失效(${this.$user.invalid_num||0})`,
					status: 2,
				}]
			},

		},
		watch: {
			current() {
				this.getList()
			}
		},
		onLoad(e) {
			this.getList()
			if (e.show == 0) {
				uni.setNavigationBarTitle({
					title: '领券中心'
				})
			}
		},
		onReachBottom() {
			this.getList('down')
		},
		methods: {
			getList(e) {
				e == 'down' ? this.page++ : this.page = 1
				let params = {
					page: this.page,
					status: this.tabs[this.current].status || 0
				}
				let api = '/member/coupon/coupon'
				if (this.$params.show == 1) api = '/member/coupon/memberCouponList'
				this.$request(api, params).then(res => {
					let list = res.data?.data || ''
					e == 'down' ? this.list.push(...list) : this.list = list
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}
</style>
